<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTenantType">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="description" label="描述" width="260" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :page-sizes="[10, 20, 30, 40, 50]"
          :total="pageInfo.total"
          :current-page="query.page"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog width="500px" title="编辑/新增" :visible.sync="TenantTypeFormVisible" :close-on-click-modal="false">
      <el-form :model="TenantTypeForm" label-width="80px" :rules="TenantTypeFormRules" ref="TenantTypeForm">
        <el-form-item label="租户名称" prop="name">
          <el-input v-model="TenantTypeForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="租户描述" prop="description">
<!--          <el-input v-model="TenantTypeForm.description" auto-complete="off"></el-input>-->
          <el-input type="textarea" v-model="TenantTypeForm.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="TenantTypeFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
  import util from "../../common/js/util";
  import {editUser} from "../../api/api";

  export default {
    data() {
      return {
        filters: {
          name: ''
        },
        listLoading: false,
        pageInfo:{
          total: 0,
          rows: []
        },
        query: {
          page: 1,
          rows: 10,
          keyword: ''
        },

        sels: [],//列表选中列

        TenantTypeFormVisible: false,//编辑界面是否显示
        editLoading: false,
        TenantTypeFormRules: {
          name: [
            { required: true, message: '请输入租户类型名称', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入租户类型描述', trigger: 'blur' }
          ]
        },
        //编辑界面数据
        TenantTypeForm: {
          id: null,
          name: '',
          description: ''
        },
      }
    },
    methods: {
      // 新增&编辑提交
      editSubmit() {
        this.$refs.TenantTypeForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              let para = Object.assign({}, this.TenantTypeForm);
              this.$http.put("/sysmanage/tenantType",para)
                .then(result => {
                  result = result.data;
                  if (result.success) {
                    this.editLoading = false;
                    this.$message({message: '提交成功', type: 'success'});
                    this.TenantTypeFormVisible = false;
                    this.getTenantType();
                  } else {
                    this.$message({message: '提交失败！'+result.message, type: 'error'});
                  }
                })
                .catch(result => {
                  this.$message({message: '网络异常！'+result.message, type: 'error'});
                })
            });
          }
        });
      },
      // 分页大小
      handleSizeChange(val) {
        this.query.rows = val;
        this.getTenantType();
      },
      // 下一页&上一页
      handleCurrentChange(val) {
        this.query.page = val;
        this.getTenantType();
      },
      // 获取租户类型分页数据
      getTenantType() {
        this.$http.post("/sysmanage/tenantType/list",this.query)
          .then(result => {
            result = result.data;
            if (result.success) {// true 后端返回了数据
              this.pageInfo = result.resultObj;
            } else {// 后端处理失败
              this.$message({message: result.message,type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: "网络错误："+result,type: 'error'});
          })
      },
      // 多选
      selsChange: function (sels) {
        this.sels = sels;
      },
      //删除
      handleDel(index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'})
          .then(() => {
            this.listLoading = true;
            //NProgress.start();
            let para = { id: row.id };
            this.$http.delete("/sysmanage/tenantType/"+para)
              .then(result => {
                result = result.data;
                if (result.success) {// true 后端返回了数据
                  this.listLoading = false;
                  //NProgress.done();
                  this.$message({message: '删除成功', type: 'success'});
                  this.getTenantType();
                } else {
                  this.$message({message: result.message,type: 'error'});
                }
              })
            })
            .catch(result => {
              this.$message({message: "网络错误："+result,type: 'error'});
            });
      },
      //显示编辑界面
      handleEdit(index, row) {
        this.TenantTypeFormVisible = true;
        this.TenantTypeForm = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd() {
        this.TenantTypeFormVisible = true;
        this.TenantTypeForm = {
          id: null,
          name: '',
          description: ''
        };
      },
      //批量删除
      batchRemove() {
        var ids = this.sels.map(item => item.id).toString();
        this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
          .then(() => {
            this.listLoading = true;
            let para = { ids: ids };
            batchRemoveUser(para).then((res) => {
              this.listLoading = false;
              this.$message({message: '删除成功', type: 'success'});
              this.getUsers();
            });
          })
          .catch(() => {
        });
      }
    },
    mounted() {
      this.getTenantType();
    }
  }

</script>

<style scoped>

</style>